package com.lge.qcontact.configuration;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.lge.qcontact.R;
import com.lge.qcontact.threads.SyncronizeContactsThread;
import com.lge.qcontact.utils.LocalDBHelper;
import com.lge.qcontact.utils.MUtils;
import android.view.Window;

public class FixMyInfoActiviy extends Activity {
	
	private EditText uIDEdit, uPWEdit, uPWCnfmEdit, uNameEdit, uMobileEdit, uEmailEdit, uPhoneNoEdit, uGroupEdit;
	private Button btnFixOK, btnFixCancel;
	
	private LocalDBHelper lHelper;
	private SQLiteDatabase localDB;
	
	private String userId;
	private String userPw;
	private String uName;
	private String phoneNo;
	private String mobile;
	private String email;
	private String isMaster;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_fix_myinfo);
		
		
		btnFixOK = (Button) findViewById(R.id.fix_ok_button);
		btnFixOK.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(uPWEdit.getText().toString().equals(uPWCnfmEdit.getText().toString())){
					UpdateThread t = new UpdateThread();
					t.start();
					try {
						t.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						MUtils.toast(getBaseContext(), "update실패. 다시 시도해 주십시오.");
					}
					
					
					SyncronizeContactsThread syncT = new SyncronizeContactsThread(getBaseContext());
					syncT.start();
					try {
						syncT.join();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					MUtils.toast(getBaseContext(), "update가 완료되었습니다.");
					finish();
				}else if(TextUtils.isEmpty(uPWCnfmEdit.getText().toString())){
					uPWCnfmEdit.setError("필수 입력 사항");
				}else{
					uPWCnfmEdit.setError("password가 틀립니다.");
				}
			}
		});
		
		
		btnFixCancel = (Button) findViewById(R.id.fix_cancel_button);
		btnFixCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		uIDEdit = (EditText) findViewById(R.id.id_fix);
		uPWEdit = (EditText) findViewById(R.id.pw_fix);
		uPWCnfmEdit = (EditText) findViewById(R.id.pw_confirm_fix);
		uNameEdit = (EditText) findViewById(R.id.name_fix);
		uMobileEdit = (EditText) findViewById(R.id.mobile_fix);
		uEmailEdit = (EditText) findViewById(R.id.email_fix);
		uPhoneNoEdit = (EditText) findViewById(R.id.phone_number_fix);
		
		lHelper = new LocalDBHelper(FixMyInfoActiviy.this);
		localDB = lHelper.getReadableDatabase();
		
		Cursor c = localDB.rawQuery("select * from LOGIN_INFO;", null);
		if(c.getCount() > 0){
			c.moveToNext();
			userId = c.getString(0);
			userPw = c.getString(1);
			
			Cursor c1 = localDB.rawQuery("select * from CONTACTS " +
					   "where uId='" + userId + "';"
					   , null);
			
			c1.moveToNext();
			
			uName = c1.getString(2);
			mobile = c1.getString(4);
			phoneNo = c1.getString(3);
			email = c1.getString(5);
			isMaster = c1.getString(6);
		}
		
		
		uNameEdit.setText(uName);
		uIDEdit.setText(userId);
		uPWEdit.setText(userPw);
		uMobileEdit.setText(mobile);
		uPhoneNoEdit.setText(phoneNo);
		uEmailEdit.setText(email);
		
	}
	
	class UpdateThread extends Thread{

		@Override
		public void run() {	//userId,userPw,mobile,email,phoneNo
			StringBuffer sql = new StringBuffer("http://192.168.0.205:8080/com.lg.project.myexample.cmd.UpdateCmd.vcc?param=");
			sql.append(uIDEdit.getText().toString()+",").
			append(uPWEdit.getText().toString()+",").
			append(uMobileEdit.getText().toString()+",").
			append(uEmailEdit.getText().toString()+",").
			append(uPhoneNoEdit.getText().toString());
			
			
			
			try {
				URL url = new URL(sql.toString());
				url.openStream();
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		

		

	}
	
}